import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

import Layout from '@/layout'

// 不需要动态判断权限的路由, 如登录页、404、等通用页面===================================================
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  { // 用户登录
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  { // 管理员登录
    path: '/login_admin',
    component: () => import('@/views/login/index_admin'),
    hidden: true
  },
  { // 企业信息
    path: '/corporation',
    component: () => import('@/views/login/corporation'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    // redirect: '/dashboard',
    redirect: '/system',
    name: 'Root_home'
    // children: [
    //   {
    //     path: 'dashboard',
    //     component: () => import('@/views/dashboard/index'),
    //     name: 'Dashboard',
    //     meta: { title: 'dashboard', icon: 'icon-shouye', affix: false } // affix, 固定在tagview中, 固定在面包屑中
    //   }
    // ]
  }
  // {
  //   path: '/documentation',
  //   component: Layout,
  //   hidden: false,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/documentation/index'),
  //       name: 'Documentation',
  //       meta: { title: 'documentation', icon: 'documentation', affix: true }
  //     }
  //   ]
  // },
  // {
  //   path: '/guide',
  //   component: Layout,
  //   redirect: '/guide/index',
  //   hidden: false,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/guide/index'),
  //       name: 'Guide',
  //       meta: { title: 'guide', icon: 'guide', noCache: true }
  //     }
  //   ]
  // },
  // {
  //   path: '/profile',
  //   component: Layout,
  //   redirect: '/profile/index',
  //   hidden: false,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/profile/index'),
  //       name: 'Profile',
  //       meta: { title: 'profile', icon: 'user', noCache: true }
  //     }
  //   ]
  // }
]

// 需求动态判断权限并通过 addRoutes 动态添加的页面===================================================
export function getMenu(perms) {
  let asyncRoutes2 = []
  if (perms === undefined || perms === null || perms.length <= 0) {
    return asyncRoutes2
  }
  asyncRoutes2 = [
    {
      path: '/system',
      component: Layout,
      redirect: '/system/account',
      name: 'System',
      hidden: !perms.includes('2'),
      meta: {
        title: '系统设置',
        icon: 'icon-xitongshezhi'
      },
      children: [
        {
          path: 'account',
          component: () => import('@/views/system/account'),
          name: 'AccountSystem',
          hidden: !perms.includes('5'),
          meta: {
            title: '账户管理',
            icon: 'icon-weibiaoti5'
          }
        },
        {
          path: 'role',
          component: () => import('@/views/system/role'),
          name: 'RoleSystem',
          hidden: !perms.includes('6'),
          meta: {
            title: '角色管理',
            icon: 'icon-huabanfuben'
          }
        },
        {
          path: 'module',
          component: () => import('@/views/system/module'),
          name: 'ModuleSystem',
          hidden: !perms.includes('7'),
          meta: {
            title: '模块管理',
            icon: 'icon-mokuaiguanli'
          }
        }
      ]
    },
    {
      path: '/client',
      component: Layout,
      redirect: '/client/clientlist',
      name: 'Client',
      hidden: !perms.includes('3'),
      meta: {
        title: '用户管理',
        icon: 'icon-yonghu'
      },
      children: [
        {
          path: 'clientlist',
          component: () => import('@/views/client/clientlist'),
          name: 'ListClient',
          hidden: !perms.includes('8'),
          meta: {
            title: '用户列表',
            icon: 'icon-ico-'
          }
        },
        {
          path: 'allocation',
          component: () => import('@/views/client/allocation'),
          name: 'AllocationClient',
          hidden: !perms.includes('9'),
          meta: {
            title: '资源分配',
            icon: 'icon-shujuku_'
          }
        }
      ]
    },
    {
      path: '/platform',
      component: Layout,
      redirect: '/platform/platformlist',
      name: 'Platform',
      hidden: !perms.includes('4'),
      meta: {
        title: '平台管理',
        icon: 'icon-pingtaiguanli'
      },
      children: [
        {
          path: 'platformlist',
          component: () => import('@/views/platform/platformlist'),
          name: 'ListPlatform',
          hidden: !perms.includes('10'),
          meta: {
            title: '平台列表',
            icon: 'icon-liebiao'
          }
        }
        // {
        //   path: 'platformlist2',
        //   component: () => import('@/views/platform/platformlist'),
        //   name: 'ListPlatform2',
        //   meta: {
        //     title: '平台列表2',
        //   }
        // },
      ]
    },

    // 404 page must be placed at the end !!!
    { path: '*', redirect: '/404', hidden: true }
  ]
  return asyncRoutes2
}

export const asyncRoutes = [
  {
    path: '/system',
    component: Layout,
    redirect: '/system/account',
    name: 'System',
    meta: {
      title: '系统设置',
      icon: 'icon-xitongshezhi'
    },
    children: [
      {
        path: 'account',
        component: () => import('@/views/system/account'),
        name: 'AccountSystem',
        meta: {
          title: '账户管理',
          icon: 'icon-weibiaoti5'
        }
      },
      {
        path: 'role',
        component: () => import('@/views/system/role'),
        name: 'RoleSystem',
        meta: {
          title: '角色管理',
          icon: 'icon-huabanfuben'
        }
      },
      {
        path: 'module',
        component: () => import('@/views/system/module'),
        name: 'ModuleSystem',
        meta: {
          title: '模块管理',
          icon: 'icon-mokuaiguanli'
        }
      }
    ]
  },
  {
    path: '/client',
    component: Layout,
    redirect: '/client/clientlist',
    name: 'Client',
    meta: {
      title: '用户管理',
      icon: 'icon-yonghu'
    },
    children: [
      {
        path: 'clientlist',
        component: () => import('@/views/client/clientlist'),
        name: 'ListClient',
        meta: {
          title: '用户列表',
          icon: 'icon-ico-'
        }
      },
      {
        path: 'allocation',
        component: () => import('@/views/client/allocation'),
        name: 'AllocationClient',
        meta: {
          title: '资源分配',
          icon: 'icon-shujuku_'
        }
      }
    ]
  },
  {
    path: '/platform',
    component: Layout,
    redirect: '/platform/platformlist',
    name: 'Platform',
    meta: {
      title: '平台管理',
      icon: 'icon-pingtaiguanli'
    },
    children: [
      {
        path: 'platformlist',
        component: () => import('@/views/platform/platformlist'),
        name: 'ListPlatform',
        meta: {
          title: '平台列表',
          icon: 'icon-liebiao'
        }
      }
      // {
      //   path: 'platformlist2',
      //   component: () => import('@/views/platform/platformlist'),
      //   name: 'ListPlatform2',
      //   meta: {
      //     title: '平台列表2',
      //   }
      // },
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
